<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\Apply;
use app\admin\model\Examtype;
use app\admin\model\Majorcourse;
use think\Request;

class  Examtypes extends Base
{
    //列表
    public function varlist(){
        $user = $this->user_info(); //判断用户是否登录
        $examtype = Examtype::order("sort","asc")->select(); //查询所有的报考类别
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => $examtype,
        );
        return view("varlist",$data);
    }
    //添加
    public function addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
        );
        return view("addform",$data);
    }
    public function add(Request $request){
        $data = $request->post();
        $data['create_times'] = time();
        $data['update_times'] = time();
        $res = Examtype::where(["name"=>$data['name']])->find();
        if(!empty($res)){
          $this->success("报考类别已经存在编号为：{$res['id']}","Examtypes/varlist");
          return true;
        }
        if(Examtype::insert($data)){
            $this->success("添加成功","Examtypes/varlist");
        }else{
            $this->error("添加失败","Examtypes/addform");
        }

    }
    //编辑
    public function update(Request $request){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "data" => Examtype::where(array('id'=>$request->get('id')))->find(),
        );
        return view("update",$data);
    }
    public function edit(Request $request){
        $data = $request->post();
        $data['update_times'] = time();
        if(Examtype::update($data)){
            $this->success("更新成功","Examtypes/varlist");
        }else{
            $this->error("更新失败","Examtypes/update");
        }

    }
    //删除
    public function delete(Request $request){
        $user = $this->user_info();
        if(Examtype::destroy($request->post('id'))){
            $this->success("删除成功","Examtypes/varlist");
        }else{
            $this->error("删除失败","Examtypes/varlist");
        }
    }
    //修改状态
    public function setStatus(Request $request){
      $data = $request->post();
      if(Examtype::update($data)){
        $this->success("成功");
      }else{
        $this->error("失败");
      }
    }

}
